<template>
  <div id="activityChart"></div>
</template>

<script setup>
import * as echarts from 'echarts';
import { onMounted } from 'vue';

onMounted(() => {
  const chartDom = document.getElementById('activityChart');
  const myChart = echarts.init(chartDom);

  const option = {
    title: {
      text: '鸟类活动频率',
      left: 'center',
      textStyle: {
        color: '#2c3e50',
        fontSize: 22,
        fontWeight: 'bold'
      },
      top: 10
    },
    tooltip: {
      trigger: 'axis',
      backgroundColor: 'rgba(255, 255, 255, 0.95)',
      borderColor: '#e0e0e0',
      borderWidth: 1,
      padding: [10, 15],
      textStyle: {
        color: '#333',
        fontSize: 14
      },
      axisPointer: {
        type: 'line',
        lineStyle: {
          color: '#999',
          width: 1,
          type: 'dashed'
        }
      },
      formatter: function (params) {
        let result = `<div style="font-weight: bold; margin-bottom: 5px;">${params[0].name}</div>`;
        params.forEach(param => {
          const unit = param.seriesName === '鸟类数量' ? '只' : '次';
          result += `
            <div style="display: flex; align-items: center; margin: 5px 0;">
              <div style="width: 10px; height: 10px; background: ${param.color}; border-radius: 50%; margin-right: 8px;"></div>
              <div>${param.seriesName}: <span style="font-weight: bold;">${param.value}${unit}</span></div>
            </div>
          `;
        });
        return result;
      }
    },
    legend: {
      data: ['鸟类数量', '活动频率'],
      bottom: 10,
      itemWidth: 15,
      itemHeight: 10,
      textStyle: {
        color: '#666',
        fontSize: 14
      },
      itemGap: 20
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '15%',
      top: '15%',
      containLabel: true
    },
    xAxis: {
      type: 'category',
      boundaryGap: false,
      data: ['00:00', '04:00', '08:00', '12:00', '16:00', '20:00'],
      axisLine: {
        lineStyle: {
          color: '#ccc'
        }
      },
      axisLabel: {
        color: '#666',
        fontSize: 12,
        margin: 10
      },
      axisTick: {
        show: false
      },
      splitLine: {
        show: true,
        lineStyle: {
          color: '#f0f0f0',
          type: 'dashed'
        }
      }
    },
    yAxis: [
      {
        type: 'value',
        name: '数量 (只)',
        nameTextStyle: {
          color: '#666',
          fontSize: 14,
          padding: [0, 0, 0, 10]
        },
        axisLine: {
          lineStyle: {
            color: '#ccc'
          }
        },
        axisLabel: {
          formatter: '{value}',
          color: '#666',
          fontSize: 12
        },
        splitLine: {
          lineStyle: {
            color: '#f0f0f0'
          }
        }
      },
      {
        type: 'value',
        name: '频率 (次)',
        nameTextStyle: {
          color: '#666',
          fontSize: 14,
          padding: [0, 10, 0, 0]
        },
        axisLine: {
          lineStyle: {
            color: '#ccc'
          }
        },
        axisLabel: {
          formatter: '{value}',
          color: '#666',
          fontSize: 12
        },
        splitLine: {
          show: false
        }
      }
    ],
    series: [
      {
        name: '鸟类数量',
        type: 'line',
        data: [5, 8, 23, 15, 18, 12],
        smooth: true,
        lineStyle: {
          width: 4,
          color: '#4CAF50',
          shadowColor: 'rgba(76, 175, 80, 0.3)',
          shadowBlur: 8,
          shadowOffsetY: 4
        },
        itemStyle: {
          color: '#4CAF50',
          borderWidth: 2,
          borderColor: '#fff'
        },
        symbol: 'circle',
        symbolSize: 8,
        label: {
          show: true,
          position: 'bottom',
          color: '#4CAF50',
          fontSize: 16,
          fontWeight: 'bold',
          formatter: '{c}只'
        },
        areaStyle: {
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            {offset: 0, color: 'rgba(76, 175, 80, 0.3)'},
            {offset: 1, color: 'rgba(76, 175, 80, 0.05)'}
          ])
        },
        animation: true,
        animationDuration: 2000,
        animationEasing: 'cubicOut'
      },
      {
        name: '活动频率',
        type: 'line',
        yAxisIndex: 1,
        data: [20, 32, 45, 28, 35, 25],
        smooth: true,
        lineStyle: {
          width: 4,
          color: '#2196F3',
          shadowColor: 'rgba(33, 150, 243, 0.3)',
          shadowBlur: 8,
          shadowOffsetY: 4
        },
        itemStyle: {
          color: '#2196F3',
          borderWidth: 2,
          borderColor: '#fff'
        },
        symbol: 'circle',
        symbolSize: 8,
        label: {
          show: true,
          position: 'top',
          color: '#2196F3',
          fontSize: 16,
          fontWeight: 'bold',
          formatter: '{c}次'
        },
        areaStyle: {
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            {offset: 0, color: 'rgba(33, 150, 243, 0.3)'},
            {offset: 1, color: 'rgba(33, 150, 243, 0.05)'}
          ])
        },
        animation: true,
        animationDuration: 2000,
        animationEasing: 'cubicOut',
        animationDelay: 300
      }
    ],
    backgroundColor: '#fff',
    textStyle: {
      fontFamily: 'Arial, sans-serif'
    }
  };

  myChart.setOption(option);

  // 响应式调整
  window.addEventListener('resize', () => myChart.resize());
});
</script>

<style scoped>
#activityChart {
  height: 500px;
  width: 350px;
  margin: 20px auto;
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.08);
  background: #fff;
  transition: all 0.3s ease;
}

#activityChart:hover {
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
  transform: translateY(-2px);
}


</style>